package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.InterestDetailForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.InterestDetailVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.UserModelBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.CustomerModelBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.LeaderModelBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.UserModelQueryForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.*;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.lightinvesting.LightinvestingService;
import com.cloudbroker.bcs.platform.ltb.middle.service.*;
import com.cloudbroker.bcs.platform.ltb.middle.util.UserTokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by world on 16/11/22.
 */
@Service
public class UserModelServiceImpl implements UserModelBizService {

    @Resource
    private LightinvestingService lightinvestingService;

    @Resource
    private ViewpointMsgService viewpointMsgService;

    @Resource
    private CommentService commentService;

    @Resource
    private UpvoteRecordService upvoteRecordService;

    @Resource
    private LeaderModelService leaderModelService;

    @Resource
    private CustomerModelService customerModelService;

    @Resource
    private UserService userService;

    public CustomerModelBack funcCustomerModel(UserModelQueryForm form) {
        CustomerModelBack back = new CustomerModelBack();
        UserEntity user = new UserEntity();
        user.setUser_id(form.getUser_id());
        user.setCompany_id(form.getCompany_id());
        user = userService.getUserEntity(user);
        if (user == null || !user.getUser_type().equals(String.valueOf(UserType.CUSTOMER.getVal()))){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.USER_NOT_EXIT,
                    CustomerModelBack.class);
        }
        back.setUser_id(form.getUser_id());
        //评论
        CommentMsg commentMsg = new CommentMsg();
        commentMsg.setUser_id(form.getUser_id());
        commentMsg.setCompany_id(form.getCompany_id());
        Integer comment_count = commentService.count(commentMsg);
        back.setComment_count(comment_count);

        //点赞数
        UpvoteRecord upvoteRecord  = new UpvoteRecord();
        upvoteRecord.setUser_id(form.getUser_id());
        Integer upvote_count  = upvoteRecordService.count(upvoteRecord);
        back.setUpvote_count(upvote_count);

        back.setQuestion_count(0);
        back.setUsed_coupon_count(0);
        //保存数据库

        CustomerModel customerModel = new CustomerModel();
        customerModel.setUser_id(back.getUser_id());
        customerModel.setCompany_id(form.getCompany_id());
        customerModel = customerModelService.getByEntity(customerModel);
        if (customerModel == null) {
            customerModel = new CustomerModel();
            customerModel.setUser_id(back.getUser_id());
            customerModel.setCompany_id(form.getCompany_id());
            customerModel.setUpvote_count(back.getUpvote_count());
            customerModel.setComment_count(back.getComment_count());
            customerModel.setQuestion_count(back.getQuestion_count());
            customerModel.setUsed_coupon_count(back.getUsed_coupon_count());
            customerModelService.save(customerModel);
        }else {
            customerModel.setUpvote_count(back.getUpvote_count());
            customerModel.setComment_count(back.getComment_count());
            customerModel.setQuestion_count(back.getQuestion_count());
            customerModel.setUsed_coupon_count(back.getUsed_coupon_count());
            customerModelService.update(customerModel);
        }
        return VOUtil.setSuccessResult(back);
    }

    public LeaderModelBack funcLeaderModel(UserModelQueryForm form) {
        LeaderModelBack back = new LeaderModelBack();
        UserEntity user = new UserEntity();
        user.setUser_id(form.getUser_id());
        user.setCompany_id(form.getCompany_id());
        user = userService.getUserEntity(user);
        if (user == null || !user.getUser_type().equals(String.valueOf(UserType.LEADER.getVal()))){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.USER_NOT_EXIT,
                    LeaderModelBack.class);
        }
        InterestDetailForm ledT2Form = new InterestDetailForm();
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(),form.getCompany_id());
        ledT2Form.setSendercomp_id(form.getCompany_id());
        ledT2Form.setUser_id(String.valueOf(form.getUser_id()));
        ledT2Form.setUser_token(user_token);
        InterestDetailVo interestDetailVo = lightinvestingService.func_interest_detail_query(ledT2Form);
        UserTokenUtil.deleteUserToken(user_token);
        back.setUser_id(form.getUser_id());
        back.setFocus_num(interestDetailVo.getFollow_num()==null?0:interestDetailVo.getFollow_num().intValue());
        back.setSign_count(interestDetailVo.getSign_num()==null?0:interestDetailVo.getSign_num().intValue());
        Long income_ratio = interestDetailVo.getIncome_ratio()==null?0:interestDetailVo.getIncome_ratio().longValue();
        back.setIncome_balance(1000000*income_ratio);
        //观点
        ViewpointMsg viewpointMsg = new ViewpointMsg();
        viewpointMsg.setUser_id(form.getUser_id());
        viewpointMsg.setCompany_id(form.getCompany_id());
        List<ViewpointMsg> viewpointMsgList = viewpointMsgService.query(viewpointMsg);
        if (viewpointMsgList!=null && viewpointMsgList.size() > 0 ){
            Integer num = 0;
            for (ViewpointMsg temp : viewpointMsgList) {
                num +=temp.getUpvote_num();
            }
            back.setViewpoint_count(viewpointMsgList.size());
            back.setViewpoint_upvote_num(num);
        }else{
            back.setViewpoint_count(0);
            back.setViewpoint_upvote_num(0);
        }
        //评论
        CommentMsg commentMsg = new CommentMsg();
        commentMsg.setUser_id(form.getUser_id());
        commentMsg.setCompany_id(form.getCompany_id());
        Integer comment_count = commentService.count(commentMsg);
        back.setComment_count(comment_count);

        back.setCombi_trans_time(0);
        back.setAnswer_count(0);
        //保存数据库
        LeaderModel leaderModel = new LeaderModel();
        leaderModel.setCompany_id(form.getCompany_id());
        leaderModel.setUser_id(back.getUser_id());
        leaderModel = leaderModelService.getByEntity(leaderModel);
        if (leaderModel == null) {
            leaderModel = new LeaderModel();
            leaderModel.setCompany_id(form.getCompany_id());
            leaderModel.setUser_id(back.getUser_id());
            leaderModel.setViewpoint_count(back.getViewpoint_count());
            leaderModel.setComment_count(back.getComment_count());
            leaderModel.setAnswer_count(back.getAnswer_count());
            leaderModel.setSign_count(back.getSign_count());
            leaderModel.setViewpoint_upvote_num(back.getViewpoint_upvote_num());
            leaderModel.setFocus_num(back.getFocus_num());
            leaderModel.setIncome_balance(back.getIncome_balance());
            leaderModel.setCombi_trans_time(back.getCombi_trans_time());
            leaderModelService.save(leaderModel);
        }else {
            leaderModel.setViewpoint_count(back.getViewpoint_count());
            leaderModel.setComment_count(back.getComment_count());
            leaderModel.setAnswer_count(back.getAnswer_count());
            leaderModel.setSign_count(back.getSign_count());
            leaderModel.setViewpoint_upvote_num(back.getViewpoint_upvote_num());
            leaderModel.setFocus_num(back.getFocus_num());
            leaderModel.setIncome_balance(back.getIncome_balance());
            leaderModel.setCombi_trans_time(back.getCombi_trans_time());
            leaderModelService.update(leaderModel);
        }
        return VOUtil.setSuccessResult(back);
    }

}
